package com.seckill.act.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seckill.act.pojo.SkuAct;
import com.seckill.act.service.SkuActService;
import com.seckill.framework.mybatisplus.WrapperParse;
import com.seckill.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 控制器</p>
 *
 * @author 作者
 * @since 2022-03-04
 */
@RestController
@RequestMapping("/skuAct")
public class SkuActController {

    @Autowired
    private SkuActService skuActService;

    /***
     * SkuAct分页条件搜索实现
     * @param skuAct
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page<SkuAct>> findPage(@RequestBody(required = false)  SkuAct skuAct, @PathVariable  int page, @PathVariable  int size){
        //调用SkuActService实现分页条件查询SkuAct
        Page<SkuAct> pageInfo = skuActService.findPage(skuAct, page, size);
        return Result.ok(pageInfo);
    }

    /***
     * SkuAct分页条件搜索实现
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{acid}/{page}/{size}" )
    public Result<Page<SkuAct>> findPageByActId(@PathVariable(value = "acid")String acid, @PathVariable  int page, @PathVariable  int size){
        //调用SkuActService实现分页条件查询SkuAct
        Page<SkuAct> pageInfo = skuActService.findPageByAcid(acid, page, size);
        return Result.ok(pageInfo);
    }

    /***
     * SkuAct分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page<SkuAct>> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用SkuActService实现分页查询SkuAct
        Page<SkuAct> pageInfo = skuActService.findPage(page, size);
        return Result.ok(pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param skuAct
     * @return
     */
    @PostMapping(value = "/search" )
    public Result<List<SkuAct>> findList(@RequestBody(required = false)  SkuAct skuAct){
        //调用SkuActService实现条件查询SkuAct
        List<SkuAct> list = skuActService.findList(skuAct);
        return Result.ok(list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){
        //调用SkuActService实现根据主键删除
        skuActService.delete(id);
        return Result.ok();
    }

    /***
     * 修改SkuAct数据
     * @param skuAct
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody  SkuAct skuAct,@PathVariable String id){
        //设置主键值
        skuAct.setSkuId(id);
        //调用SkuActService实现修改SkuAct
        skuActService.update(skuAct);
        return Result.ok();
    }

    /***
     * 新增SkuAct数据
     * @param skuAct
     * @return
     */
    @PostMapping
    public Result add(@RequestBody   SkuAct skuAct) throws Exception {
        //调用SkuActService实现添加SkuAct
        skuActService.add(skuAct);
        return Result.ok();
    }

    /***
     * 根据ID查询SkuAct数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<SkuAct> findById(@PathVariable String id){
        //调用SkuActService实现根据主键查询SkuAct
        SkuAct skuAct = skuActService.findById(id);
        return Result.ok(skuAct);
    }

    /***
     * 查询SkuAct全部数据
     * @return
     */
    @GetMapping
    public Result<List<SkuAct>> findAll(){
        //调用SkuActService实现查询所有SkuAct
        List<SkuAct> list = skuActService.findAll();
        return Result.ok(list);
    }

    /**
     *根据时间查询
     */
    @DeleteMapping(value = "/del/{aid}/{sid}")
    public Result delByASId(@PathVariable(value = "aid")String aid,@PathVariable(value = "sid")String sid){
        QueryWrapper<SkuAct> delWrapper = WrapperParse.simpleColumns("activity_id,sku_id",aid,sid);
        skuActService.remove(delWrapper);
        return Result.ok();
    }

    //findSkuById
    @GetMapping(value = "/findSkuById/{id}")
    public Result<List<String>> findSkuById(@PathVariable(value = "id")String id){
        List<String> ids = skuActService.findSkuById(id);
        return Result.ok(ids);
    }
}

